const { ccclass, property } = cc._decorator;
import WorldManager from './WorldNodeControl';
import WayBaseControl from './WayBaseControl';
import MyEnums from '../MyEnums';

@ccclass
export default class flyWayControl extends WayBaseControl {
    @property
    length: number = 1;
    @property(cc.SpriteFrame)
    beginSprite: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    endSprite: cc.SpriteFrame = null;
    @property([cc.SpriteFrame])
    midSprite: cc.SpriteFrame[] = [];
    @property
    maxY: number = 500;

    worldManager: WorldManager = null;
    _PhysicsBody: cc.RigidBody = null;
    cameraX_nodeX = 0;

    onLoad() {
        this.setLongth();
        this.setCollider(true,TagWayBody2);
        this.setRigidBody();
        if (this.node.y > this.maxY) {
            this.node.y = this.maxY
        }
        else if (this.node.y < 0) {
            this.node.y = 0;
        }
        this.cameraX_nodeX = (cc.winSize.width + this.node.width) / 2;
    }
    setLongth() {
        let nodes: Array<cc.Node> = new Array<cc.Node>();
        if (this.length <= 1) {
            let nodeBegin = new cc.Node();
            nodeBegin.group = this.node.group;
            let sprite1 = nodeBegin.addComponent(cc.Sprite);
            sprite1.spriteFrame = this.beginSprite;
            nodes.push(nodeBegin);

            let nodeEnd = new cc.Node();
            nodeEnd.group = this.node.group;
            let sprite2 = nodeEnd.addComponent(cc.Sprite);
            sprite2.spriteFrame = this.endSprite;
            nodes.push(nodeEnd);
        }
        else {
            nodes = this.getMidNode();
            let nodeBegin = new cc.Node();
            nodeBegin.group = this.node.group;
            let sprite1 = nodeBegin.addComponent(cc.Sprite);
            sprite1.spriteFrame = this.beginSprite;
            nodes.unshift(nodeBegin);
            let nodeEnd = new cc.Node();
            nodeEnd.group = this.node.group;
            let sprite2 = nodeEnd.addComponent(cc.Sprite);
            sprite2.spriteFrame = this.endSprite;
            nodes.push(nodeEnd);
        }
        if (nodes.length > 0) {
            this.addNodes(nodes);
            this.nodeLength = nodes.length - 1;//首尾算一个
            if (this.nodeLength == 1) {
                this.indexWidth = this.node.width;
            }
            else {
                let index = Math.floor(nodes.length / 2);
                this.indexWidth = nodes[index].width;
            }
            for (let i = 0; i < nodes.length; i++) {
                this.occupyList.push(false);
            }
        }
    }
    private getMidNode(): Array<cc.Node> {
        let nodes: Array<cc.Node> = new Array<cc.Node>();
        let lenM = this.midSprite.length;
        for (let i = 0; i < this.length - 1; i++) {
            let node = new cc.Node();
            node.group = this.node.group;
            let sprite2 = node.addComponent(cc.Sprite);
            sprite2.spriteFrame = this.midSprite[i % lenM];
            nodes.push(node);
        }
        return nodes;
    }
    private addNodes(nodes: Array<cc.Node>) {
        let nodeWidth: number = 0;
        for (let i = 0; i < nodes.length; i++) {
            nodeWidth += nodes[i].width;
        }
        nodeWidth -= nodes.length;//每个减去1像素，防止偶尔出现裂缝
        let nodex = nodeWidth / 2;
        for (let i = 0; i < nodes.length; i++) {
            let node = nodes[i];
            let width = node.width - 1;//每个减去1像素，防止偶尔出现裂缝
            node.x = width / 2 - nodex;
            nodex -= width;
            node.name = "way";
            this.node.addChild(node, i);
        }
        this.node.width = nodeWidth - 15;
        this.node.height = nodes[0].height;
    }
    setRigidBody() {
        let rigidBody = this.node.getComponent(cc.RigidBody);
        this._PhysicsBody = rigidBody;
        rigidBody.fixedRotation = true;
        rigidBody.enabledContactListener = true;
        rigidBody.linearVelocity = new cc.Vec2(this.speed, 0);
    }
    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsBoxCollider, otherCollider: cc.PhysicsBoxCollider) {
        if (otherCollider.body.linearVelocity.x == 0 && otherCollider.body.linearVelocity.y == 0) {
            this.speed = 0 - this.speed
            selfCollider.body.linearVelocity = new cc.Vec2(this.speed, 0);
        }
        else {
            let normal = contact.getWorldManifold().normal;
            if (normal.x != 0) {
                if (this.speed > 0 && normal.x > 0) {
                    this.speed = 0 - this.speed
                    selfCollider.body.linearVelocity = new cc.Vec2(this.speed, 0);
                }
                if (this.speed < 0 && normal.x < 0) {
                    this.speed = 0 - this.speed
                    selfCollider.body.linearVelocity = new cc.Vec2(this.speed, 0);
                }
            }
        }
    }
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsBoxCollider, otherCollider: cc.PhysicsBoxCollider) {
        if (otherCollider.tag == TagHeroBody) {
            HeroManager.getInstance().flyWaySpeed = 0;
        }
    }
    onPostSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsBoxCollider, otherCollider: cc.PhysicsBoxCollider) {
        if (otherCollider.tag == TagHeroBody) {
            HeroManager.getInstance().flyWaySpeed = this.speed;
        }
    }
    update(dt) {
        let hero = HeroHelper.getInstance();
        let xx = Math.abs(this.node.x - hero.cameraPosition.x);
        if (xx < this.cameraX_nodeX) {
            this.worldManager.SetWayActive(true, this.node.uuid, 'fly');
        }
        else {
            this.worldManager.SetWayActive(false, this.node.uuid, 'fly');
        }

        if (this.node.y > this.maxY) {
            this.speed = 0 - this.speed;
            this._PhysicsBody.linearVelocity = new cc.Vec2(0, this.speed);
        }
    }
}
